/**
 * 作者:李杰
 * email: maillijie@gmail.com
 */
package com.javainfo.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.javainfo.dao.inf.ArticleDaoIf;
import com.javainfo.entity.Article;
import com.javainfo.entity.User;

public class ArticleDao extends BaseDao<Article> implements ArticleDaoIf{
	@Override
	public void add(Article obj) {
		Date createDate = new Date();
		obj.setCreateTime(createDate);
		obj.setUpdateTime(createDate);
		super.add(obj);
	}
	
	@Override
	public void update(Article obj) {
		Date createDate = new Date();
		obj.setUpdateTime(createDate);
		super.update(obj);
	}

	@SuppressWarnings("unchecked")
	public List<Article> serachArticleByUserAccount(String userAccount) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Article.class);
		List<Article> articles = new ArrayList<Article>();
		User exampleEntity = new User();
		exampleEntity.setUserAccount(userAccount);
		List<User> list = getHibernateTemplate().findByExample(exampleEntity);
		if (list != null && list.size() == 1) {
			criteria.add(Restrictions.eq("user", list.get(0)));
			articles = getHibernateTemplate().findByCriteria(criteria);
		}
		return articles;
	}
}
